var PersonLiableSetting = function () {
  var PersonShareSetting = function () {
    var template = /* html */ `
      <div class="person-share-setting">
        <div class="share-setting-title">设置责任比例(比例总和必须为100%)</div>
        <div class="share-setting-content">
          <el-form inline size="medium" label-width="auto">
            <el-form-item v-for="person in personList" :key="person.id" :label="person.name">
              <el-input-number class="setting-input" is-decimal-zero v-model="person.share" :controls="false" :min="0" :max="100" :precision="2">
              </el-input-number>
              <span>%</span>
            </el-form-item>
          </el-form>
          <div style="text-align:center">
            <el-button size="medium" type="primary" @click="ensure">确定</el-button>
          </div>
        </div>
      </div>
    `;

    return {
      props: {
        users: {
          type: Object,
          required: true
        }
      },

      data: function () {
        return {
          personList: this.getPersonList()
        };
      },

      methods: {
        getPersonList: function () {
          var users = this.users;
          users = _.cloneDeep(users);
          return users;
        },

        ensure: function () {
          var personList = this.personList;
          var sum = 0;
          for (var i = 0; i < personList.length; i++) {
            var person = personList[i];
            var share = person.share;
            if (share === undefined) {
              this.$alert('请输入责任比例', '信息');
              return;
            }

            if (share === 0) {
              this.$alert('责任比例不能为0', '信息');
              return;
            }
            sum += share;
          }

          sum = _.round(sum, 2);
          if (sum !== 100) {
            this.$alert('责任比例总和必须为100%', '信息');
            return;
          }

          this.$emit('share-success', personList);
        }
      },

      template: template
    };
  }();

  var template = /* html */ `
    <div class="person-liable-setting" v-loading.fullscreen.lock="loading">
      <transition name="el-fade-in">
        <div v-if="step===1" class="person-select-content">
          <el-transfer filterable class="person-transfer" v-model="selectIds" :data="userList" :props="optionProps"
            :titles="['未选中','已选中']">
          </el-transfer>
          <div style="text-align:center;margin-top:10px">
            <el-button size="medium" type="primary" @click="ensure">确定</el-button>
          </div>
        </div>
        <person-share-setting v-else-if="step===2" :users="selectUserList" @share-success="shareSuccess"></person-share-setting>
      </transition>
    </div>
  `;

  return {
    props: {
      order: {
        type: Object,
        required: true
      },
      dropdownOption: {
        type: Object,
        default: function () {
          return {};
        }
      }
    },

    data: function () {
      var personLiableList = this.order.children[0].personLiableList;
      var selectIds = _.map(personLiableList, 'id');
      return {
        loading: false,
        selectIds: selectIds,
        step: 1,
        userList: [],
        selectUserList: [],
        optionProps: {
          key: 'id',
          label: 'name'
        }
      };
    },

    created: function () {
      this.setUserList();
    },

    methods: {
      //设置可选责任人
      setUserList: function () {
        var dropdownOption = this.dropdownOption;
        var userList = dropdownOption.userList;
        if (userList !== undefined) {
          this.userList = userList;
          return;
        }

        this.loading = true;
        var self = this;
        ajaxRequest('applyRepairForm/getWorkShopUser').then(function (userList) {
          userList = _.map(userList, function (user) {
            return {
              id: user.id,
              name: user.userName
            };
          });
          dropdownOption.userList = userList;
          self.userList = userList;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      ensure: function () {
        var selectIds = this.selectIds;
        if (selectIds.length === 0) {
          this.$alert('至少选择一个责任人', '信息');
          return;
        }

        var userList = this.userList;
        userList = _.filter(userList, function (user) {
          return selectIds.indexOf(user.id) !== -1;
        });

        if (userList.length === 1) {
          var user = userList[0];
          var personLiableList = [{
            id: user.id,
            name: user.name,
            share: 100
          }];
          this.order.children[0].personLiableList = personLiableList;
          this.$emit('setting-success', this.order);
          return;
        }
        this.selectUserList = userList;
        this.step = 2;
      },

      shareSuccess: function (personLiableList) {
        this.order.children[0].personLiableList = personLiableList;
        this.$emit('setting-success', this.order);
      }
    },

    components: {
      PersonShareSetting: PersonShareSetting
    },

    template: template
  };
}();
